import {config} from './config'
import {useCache} from '@/hooks/web/useCache'
import service from "@/utils/request";

import request from 'sync-request';
import {getToken} from "@/utils/auth";

const {default_headers} = config
const {wsCache} = useCache()
console.log('lang=' + wsCache.get('lang'));
const language = wsCache.get('lang')
const asyncRequest = (option: any) => {
    const {url, method, params, data, headersType, responseType, ...config} = option
    return service({
        url: url,
        method,
        params,
        data,
        ...config,
        responseType: responseType,
        headers: {
            'Content-Type': headersType || default_headers,
            'language': language
        }
    })
}
export default {
    getSync: <T = any>(option: any) => {
        const isToken = (option.headers || {}).isToken === false;
        if (getToken() && !isToken) {
            option.headers = option.headers || {};
            option.headers['Authorization'] = 'Bearer ' + getToken(); // 让每个请求携带自定义token 请根据实际情况自行修改
        }

        const {url} = option


        const response = request('GET', import.meta.env.VITE_APP_BASE_API + url,
            option);
        console.log(response.getBody('utf8'));

        const res = JSON.parse(response.getBody('utf8'))
        return res
    },
    get: async <T = any>(option: any) => {
        const res = await asyncRequest({method: 'GET', ...option})
        return res.data as unknown as T
    },
    post: async <T = any>(option: any) => {
        const res = await asyncRequest({method: 'POST', ...option})
        return res.data as unknown as T
    },
    postOriginal: async (option: any) => {
        const res = await asyncRequest({method: 'POST', ...option})
        return res
    },
    delete: async <T = any>(option: any) => {
        const res = await asyncRequest({method: 'DELETE', ...option})
        return res.data as unknown as T
    },
    enable: async <T = any>(option: any) => {
        const res = await asyncRequest({method: 'POST', ...option})
        return res.data as unknown as T
    },
    disable: async <T = any>(option: any) => {
        const res = await asyncRequest({method: 'POST', ...option})
        return res.data as unknown as T
    },
    put: async <T = any>(option: any) => {
        const res = await asyncRequest({method: 'PUT', ...option})
        return res.data as unknown as T
    },
    download: async <T = any>(option: any) => {
        const res = await asyncRequest({method: 'GET', responseType: 'blob', ...option})
        return res as unknown as Promise<T>
    },
    downloadPost: async <T = any>(option: any) => {
        const res = await asyncRequest({method: 'POST', responseType: 'blob', ...option})
        return res as unknown as Promise<T>
    },
    upload: async <T = any>(option: any) => {
        option.headersType = 'multipart/form-data'
        const res = await asyncRequest({method: 'POST', ...option})
        return res as unknown as Promise<T>
    }
}
